<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/20
 * Time: 10:56
 */

namespace App\Tools;

use \JPush\Client as JPush;

class Push
{
    protected $app_key = '';
    protected $app_secret = '';

    public function __construct()
    {
        // \think\Log::write('进入个推构造方法：','log',true);
        //  极光推送的配置
        $this->app_key = config('jpush.app_key');
        $this->app_secret = config('jpush.master_secret');
    }


    /**
     * 向特定设备推送消息
     * @param $regid
     * @param $alert
     * @param $data
     * @return mixed
     */
    public function pushSpecial($regid, $alert, $data)
    {
        // \think\Log::write('进入个推','log',true);

        $client = new JPush($this->app_key, $this->app_secret);

        $extras = $this->getExtraData($data);

        $androidParam = [
            "builder_id" => 1,
            "style" => 0,
            "priority" => 1,
            "extras" => $extras,
        ];

        $iosParam = [
            "sound" => 'sub.caf',
            "extras" => $extras,
        ];

        $platform = ['ios', 'android'];

        $options = [
            'apns_production' => true,
        ];

        try {
            $result = $client->push()
                ->setPlatform($platform)
                // ->options(['apns_production'=>true])//苹果生产环境
                ->addRegistrationId($regid)
                ->iosNotification($alert, $iosParam)
                ->androidNotification($alert, $androidParam)
                ->options($options)
                ->send();
              //\Log::info('推送记录：' . var_export($result, true));
        } catch (\Exception $e) {
            $result = [];
            \Log::info('推送失败：' . $e->getMessage());
        }
       
        return $this->json_array($result);
    }

    public function getExtraData($data = [])
    {
        switch ($data['action'])
        {
            case 'rectification_details':
                $extras = [
                    "target" => "local",
                    "link" => $data['action'] . "?id=" . $data['id'],
                ];
                break;
            case 'fortune_for_month':
                $extras = [
                    "target" => "local",
                    "link" => $data['action'] . "?rid=" . $data['rid'] ."&time=".$data['time'],
                ];
                break;
            case 'fortune_for_year':
                $extras = [
                    "target" => "local",
                    "link" => $data['action'] . "?rid=" . $data['rid'] ."&time=".$data['time'],
                ];
                break;
             case 'getCommunityInfo':
                $extras = [
                    "target" => "local",
                    "link" => $data['action'] . "?id=" . $data['id'],
                ];
                break;
            case 'article_details':
                $extras = [
                    "target" => "local",
                    "link" => $data['action'] . "?id=" . $data['id'],
                ];
                break;
            default :
                $extras = [];
                break;
        }
        return $extras;
    }

    /**
     * 将数据先转换成json,然后转成array
     * @param $result
     * @return mixed
     */
    private function json_array($result)
    {
        $result_json = json_encode($result);
        return json_decode($result_json, true);
    }
}
